package tv.youxing.android.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.kymjs.rxvolley.RxVolley;
import com.kymjs.rxvolley.client.HttpCallback;
import com.kymjs.rxvolley.client.HttpParams;
import com.kymjs.rxvolley.http.VolleyError;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import tv.youxing.android.MainActivity;
import tv.youxing.android.R;
import tv.youxing.android.custom.CustomCountDownTimer;
import tv.youxing.android.custom.CustomToast;
import tv.youxing.android.entity.PhoneCodeModel;
import tv.youxing.android.entity.UserPwdLoginModel;
import tv.youxing.android.utils.AppConst;
import tv.youxing.android.utils.GsonUtils;
import tv.youxing.android.utils.L;
import tv.youxing.android.utils.ShareUtils;
import tv.youxing.android.utils.StaticClass;
import tv.youxing.android.utils.UtilTools;

import static tv.youxing.android.R.id.phone;
import static tv.youxing.android.utils.GsonUtils.GsonToBean;

/**
 * Created by apple on 2017/4/24.
 */

/**
 * 项目名：   youxing
 * 包名：     tv.youxing.android .fragment
 * 文件名：   PhoneLoginFragment
 * 创建者：   叶克武
 * 创建时间： 2017/4/24 下午2:37
 * 描述：    TODO
 */
public class PhoneLoginFragment extends Fragment {


    @BindView(phone)
    EditText et_phone;
    @BindView(R.id.sendCode)
    EditText et_code;
    @BindView(R.id.btn_sendCode)
    Button btnSendCode;
    @BindView(R.id.btn_login)
    Button btnLogin;
    Unbinder unbinder;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_phone_login, null);
        findView(view);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    private void findView(View view) {


    }


    @OnClick({R.id.btn_sendCode,R.id.btn_login})
    public void click(View v) {

        switch (v.getId()) {
            case  R.id.btn_sendCode:

                sendCode();

                break;
            case R.id.btn_login:

                login();

                break;
        }

    }

    private void login() {

        String phone = UtilTools.getString(et_phone);
        String code = UtilTools.getString(et_code);


        if (!UtilTools.isPhoneSuccess(phone)||phone.length()<=0) {
            CustomToast.INSTANCE.showToast("手机号码格式不正确");
            return;
        }

        if (code.length() <= 0) {
            CustomToast.INSTANCE.showToast("验证码不能为空");
            return;
        }



        //校对验证码
        HttpParams params = new HttpParams();
        params.put("mobile", phone);
        params.put("code", code);
        params.put("login_type", 2);


        L.d("phone:" + phone);
        L.d("code:" + code);



        RxVolley.post(StaticClass.userLoginURL, params, new HttpCallback() {
            @Override
            public void onSuccess(String t) {

                UserPwdLoginModel userPwdLoginModel = GsonToBean(t, UserPwdLoginModel.class);
                CustomToast.INSTANCE.showToast(userPwdLoginModel.getStatus());
                if (userPwdLoginModel.getCode() == 200) {
                    ShareUtils.putString(getContext(), AppConst.TOKEN,userPwdLoginModel.getResult().getToken());
                    ShareUtils.putString(getContext(), AppConst.USERNAME,userPwdLoginModel.getResult().getUsername());
                    ShareUtils.putInt(getContext(), AppConst.USERID,userPwdLoginModel.getResult().getUser_id());
                    ShareUtils.putString(getContext(), AppConst.AVATER,userPwdLoginModel.getResult().getAvatar());
                    ShareUtils.putBoolean(getContext(),AppConst.ISLOGIN,true);

//                    ReservoirUtils.putString(AppConst.TOKEN,userPwdLoginModel.getResult().getToken());

                    Intent intent = new Intent(getActivity(), MainActivity.class);

                    startActivity(intent);

                    getActivity().finish();

                }else{
                    L.d("登录失败,请检查手机号或者验证码错误:"+t);
                }
            }



            @Override
            public void onFailure(VolleyError error) {
                L.d("error:" + error.getMessage());
            }
        });

    }


    /**
     * 发送验证码
     */
    void sendCode() {
        String phone = UtilTools.getString(et_phone);

        if (!UtilTools.isPhoneSuccess(phone)||phone.length()<=0) {
            CustomToast.INSTANCE.showToast("手机号码格式不正确");
            return;
        }



        HttpParams params = new HttpParams();
        params.put("mobile", phone);

        RxVolley.post(StaticClass.sendCheckCodeURL, params, new HttpCallback() {
            @Override
            public void onSuccess(String t) {


                PhoneCodeModel pd =GsonUtils.GsonToBean(t, PhoneCodeModel.class);
                CustomToast.INSTANCE.showToast(pd.getStatus());
                if (pd.getCode() == 200) {
//                    CustomToast.INSTANCE.showToast("验证码发送成功");
                    CustomCountDownTimer customCountDownTimer = new CustomCountDownTimer(60000,1000,btnSendCode);
                    customCountDownTimer.start();
                }else{
//                    CustomToast.INSTANCE.showToast("error:" + pd.getCode() + "---" + pd.getStatus());
                }
            }

            @Override
            public void onFailure(VolleyError error) {
                super.onFailure(error);
                L.d("errorCode:" + error.getMessage());
                CustomToast.INSTANCE.showToast(error.getMessage());
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
